/**
 * Created by lenovo on 2018/1/8.
 */
/**
 * Created by lenovo on 2018/1/8.
 */
define(['ngApp', 'ngHttp', 'jquery', 'ngConstant', 'ngAlert', 'bootstrap'], function (ngApp, ngHttp, JQ, ngConstant, ngAlert) {
    ngApp.directive('onFinishRenderFilters', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function () {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        }
    });
    ngApp.controller("leftTab8_2", function ($scope, $http, $timeout, $rootScope, $state, $uibModal, commonFn, $cookies, $window) {
        //-------------------------------------------------车型部分----------------------------------------------

        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            $rootScope.leftTabHeight = $('#leftTab8_2').height();
            $('.tabBox').height($rootScope.leftTabHeight);
            console.log($rootScope.leftTabHeight);
        });

        //查询数据的函数封装
        function getCarModelData(){
            if (!$scope.searchClicked) {
                    //查询table中所有数据接口
            $http({
                url: ngConstant.interfaceHost + 'manager/carStyle/findSearch',
                method: 'GET',
                params:{
                    "sort":"leadingChar,asc",
                    "page":$scope.page,
                    "size":ngConstant.size
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg  = res.data.data;
                console.log(res.data.data.content);
                //表格数据
                $scope.carModel = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.carModel, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.carModel, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    console.log($event);
                    angular.forEach($scope.carModel, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.carModel.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
            } else {
                $scope.searchMsg.page = $scope.page;
                $scope.searchMsg.size = ngConstant.size;
                $scope.searchMsg.sort = "leadingChar,asc";

                //查询table中所有数据接口
            $http({
                url: ngConstant.interfaceHost + 'manager/carStyle/findSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg  = res.data.data;
                console.log(res.data.data.content);
                //表格数据
                $scope.carModel = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.carModel, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.carModel, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    console.log($event);
                    angular.forEach($scope.carModel, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.carModel.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
            }
            
        }

        //年份
        var list = {};
        list.arr =
            [
                2017, 2016, 2015, 2014, 2013, 2012 ,2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001,2000
            ];

        $scope.listYear = list.arr;



        //点击增加出弹层
        $scope.createCarStyleBtn = function(){
            $scope.layerOutShow = true;
            //弹层中品牌 数据  接口获取
            $http({
                url: ngConstant.interfaceHost + 'admin/carBrand/findAllNormalBrand',
                method: 'GET',
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                $scope.carStyle = res.data.data;
                $scope.carStyleCode =  $scope.carStyle[0].code;
                console.log($scope.carStyleCode);
                $http({
                    url: ngConstant.interfaceHost + 'admin/carModel/findAllNormalModelByBrandCode',
                    method: 'GET',
                    params:{
                        "brandCode":$scope.carStyleCode
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    $scope.carType = res.data.data;
                    $scope.carTypeCode =  $scope.carType[0].code;
                });

            });

            $scope.creatCarStyleMsg = {};
            //弹层select中根据品牌获取车型
            $scope.getCarStyleLeadingChar = function(){
                angular.forEach($scope.carStyle, function (i) {
                    if (i.code == $scope.carStyleCode) {
                        $scope.creatCarStyleMsg.leadingChar = i.leadingChar;
                    }
                });

                $http({
                    url: ngConstant.interfaceHost + 'admin/carModel/findAllNormalModelByBrandCode',
                    method: 'GET',
                    params:{
                        "brandCode":$scope.carStyleCode
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    $scope.carType = res.data.data;
                    $scope.carTypeCode =  $scope.carType[0].code;
                });

            };

            $scope.creatCarStyleSub = function(){
                var carBrandObj = document.getElementsByName("carStyleName")[0];
                var carBrandbel= carBrandObj .options[carBrandObj.selectedIndex].text;
                $scope.creatCarStyleMsg.brandCode = carBrandbel;
                var carBrandObj1 = document.getElementsByName("carTypeName")[0];
                var carBrandbel1= carBrandObj1 .options[carBrandObj1.selectedIndex].text;
                $scope.creatCarStyleMsg.carModel = carBrandbel1;

                var yearselect = document.getElementsByName("carYearName")[0];
                var yearbel= yearselect .options[yearselect.selectedIndex].text;
                $scope.creatCarStyleMsg.year = yearbel;

                if (!$scope.creatCarStyleMsg.leadingChar) {
                    $scope.creatCarStyleMsg.leadingChar = $scope.carStyle[0].leadingChar;
                }
                console.log($scope.creatCarStyleMsg);
                $http({
                    url: ngConstant.interfaceHost + 'manager/carStyle/createCarStyle',
                    method: 'POST',
                    data:$scope.creatCarStyleMsg,
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    ngAlert.successMsg(res.data.data);
                    $timeout(function(){
                        $scope.layerOutShow = false;
                    },2000);
                    getCarModelData();
                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };


        };


        //搜索
        $scope.searchMsg = {};
        $scope.searchMsg.size = ngConstant.size;
        $scope.searchClicked = false;
        $scope.searcBtnClick = function(){
            $scope.searchClicked = true;
            console.log($scope.searchMsg);
            $http({
                url: ngConstant.interfaceHost + 'manager/carStyle/findSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                // if(res.data.data.content.length == 0) {
                //     ngAlert.successMsg("暂无数据");
                // } else {
                //     $scope.pageMsg = res.data.data;
                //     $scope.carModel = res.data.data.content;
                // }
                $scope.pageMsg = res.data.data;
                $scope.carModel = res.data.data.content;

            });
        };


        //获取table列表
        $scope.page = 0;
        $scope.page1 = $scope.page + 1;
        $http({
            url: ngConstant.interfaceHost + 'manager/carStyle/findSearch',
            method: 'GET',
            params:{
                "sort":"leadingChar,asc",
                "page":$scope.page,
                "size":ngConstant.size
            },
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            console.log(res);
            //页数数据
            $scope.pageMsg  = res.data.data;
            $scope.carModel = res.data.data.content;
            //全选
            //点击下一页上一页时   $scope.select_all初始化置空   在点击事件中写入
            $scope.checked = [];
            $scope.checkedMsg = [];
            $scope.selectAll  = function () {
                if($scope.select_all) {
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    angular.forEach($scope.carModel, function (i) {
                        i.checked  = true;
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    })
                }else {
                    angular.forEach($scope.carModel, function (i) {
                        i.checked  = false;
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                    })
                }
            };
            $scope.selectOne = function ($event) {
                console.log($event);
                angular.forEach($scope.carModel, function (i) {
                    var index = $scope.checked.indexOf(i.id);
                    if(i.checked  && index === -1) {
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    } else if (!i.checked  && index !== -1){
                        $scope.checked.splice(index, 1);
                        $scope.checkedMsg.splice(index, 1);
                    }
                });

                if ($scope.carModel.length === $scope.checked.length) {
                    $scope.select_all  = true;
                } else {
                    $scope.select_all  = false;
                }
            };

            //下一页
            $scope.nextPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1) {
                    return false;
                } else {
                    $scope.page++;
                    $scope.page1++;
                    getCarModelData();
                }

            };

            //上一页
            $scope.prevPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                } else {
                    $scope.page--;
                    $scope.page1--;
                    getCarModelData();
                }
            };

            //转到第几页
            $scope.myKeyup13  = function(e){
                $scope.select_all = false;
                //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
                var keycode = window.event?e.keyCode:e.which;
                if(keycode==13){
                    $scope.page = $scope.page1 - 1;
                    getCarModelData();
                }
            };

            //首页
            $scope.firstPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                }
                $scope.page = 0;
                $scope.page1 = 1;
                getCarModelData();
            };

            //尾页
            $scope.lastPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1){
                    return false;
                }
                $scope.page = $scope.pageMsg .totalPages - 1;
                $scope.page1 = $scope.pageMsg .totalPages;
                getCarModelData();
            };

            //批量删除品牌
            $scope.deleteCarModelBtn = function(){
                if ($scope.checkedMsg.length == 0) {
                    ngAlert.errorMsg('请选择要删除的数据');
                    return false;
                }
                ngAlert.open('确认删除本条记录？');
                console.log($scope.checkedMsg);
                $('.layui-layer-btn').click(function(){
                    $http({
                        url: ngConstant.interfaceHost + 'manager/carStyle/deleteCarStyle',
                        method: 'POST',
                        data:angular.toJson($scope.checkedMsg) ,
                        headers: {'Authorization':$cookies.get("token")}
                    }).then(function(res){
                        console.log(res);
                        ngAlert.successMsg(res.data.data);
                        getCarModelData();

                    });
                });
            };

            //点击禁用 / 启用
            $scope.disableBtn  = function(){
                this.item.dataSign = 1;
                console.log(this.item);
                $http({
                    url: ngConstant.interfaceHost + 'manager/carStyle/disable',
                    method: 'POST',
                    data:{"id":this.item.id},
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    ngAlert.successMsg(res.data.data);

                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };
            $scope.enDisableBtn  = function(){
                this.item.dataSign = 0;
                $http({
                    url: ngConstant.interfaceHost + 'manager/carStyle/enable',
                    method: 'POST',
                    data:{"id":this.item.id},
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    ngAlert.successMsg(res.data.data);

                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };

        });


        $scope.closeLay = function(){
            $scope.layerOutShow = false;
        }
    });
});